/**
 * Rabbit 2013-3-3
 * HChart 工具包，用于计算辅助 HChart
 * 用于计算中英单位以及Chart数据源转换等
 * 注意：在Chart周期重绘时切换带有Chart的模块要先停止Chart的绘制
 */

// Ext.namespace('webapp.common');

var HChartUtil = {
	ChartUnit : null,
	Timer : null,
	ParamsMapping : null
}; 

(function() {

	/**
	 * 定时器
	 */
	HChartUtil.Timer = function(cfg) {
		//执行周期   及时：5秒, 1小时：30秒, 24小时：10分钟
		this.period = cfg.period ? cfg.period : null;
		// unit:秒
		this.interval = 30 * 1000;
		this.callback = cfg.callback ? cfg.callback : null;
		this.param = cfg.param ? cfg.param : null;
		this.timer = null;

		this.init = function() {
			//debugger;
			switch( this.period ) {
				case 'current':
					this.interval = 5 * 1000;
					break;
				case 'hour':
					this.interval = 30 * 1000;
					break;
				case 'day':
					this.interval = 10 * 60 * 1000;
					break;
				case 'month':
					this.interval = 0;
					break;
				default:
					//alert("error period");
					break;
			}
		};

		this.setPeriod = function(period) {
			this.stop();
			this.period = period;
			this.init();
			//this.start();
		};

		this.setParam = function(param) {
			this.param = param;
		};

		this.setCallBack = function(func) {
			this.callback = func;
		};

		this.start = function() {
			if(this.interval == 0)
				return;
			if(this.param) {
				this.timer = window.setInterval(this.callback, this.interval, this.param);
			} else {
				this.timer = window.setInterval(this.callback, this.interval);
			}
		};

		this.stop = function() {
			try {
				clearInterval(this.timer);
			} catch(e) { debugger;
			}
		}

		this.init();
	};
	/**
	 * Chart 单位运算
	 * TODO  Math.pow(1000,3) 次方函数
	 */
	HChartUtil.ChartUnit = new function() {
	
		var _scale = 1, _unit = '', _minValue = 0, _maxValue = 0;
	
		var _smallestValue = function(array) {
			return Math.min.apply( Math, array );
		}
		var _largestValue = function(array) {
			return Math.max.apply( Math, array );
		}
		var _dataConverter = function(chartData) {
			if(jQuery.isArray(chartData)) {
				var compareList = [];
				//debugger;
				for(var i=0; i<chartData.length; i++) {
					var item = chartData[i];
					if(jQuery.isArray(item.data)) {
					var subItemList = [];
					for(var j=0; j<item.data.length; j++) {
						var subItem = item.data[j];
						if(jQuery.isArray(subItem)) { //数组
							subItemList.push(subItem[1]);
						} else if(jQuery.isPlainObject(subItem)) { //对象
							subItemList.push(subItem['y']);
						} else if(jQuery.isNumeric(subItem)) { //数字
							subItemList.push(subItem);
						}
					}
					compareList = compareList.concat(subItemList);
					}
				}
				return compareList;
			}
			return null;
		}
		/**
		* 计算最大最小值
		*/
		var _calcMaxMinValue = function(chartData) {
			var cDataList = _dataConverter(chartData);
			//debugger;
			var sValue = _smallestValue(cDataList);
			var lValue = _largestValue(cDataList);
		
			return {minValue:sValue, maxValue:lValue};
		}
	
		/**
		* 计算刻度比例和单位
		*/
		var _calcScaleAndUnit = function(displayDataType, maxValue, currentLang) {
			switch(displayDataType) {
				case 'traffic': //流速
					var trafficSUObj = _getTrafficScaleAndUnitByMaxValue(maxValue,'bps');
					_scale = trafficSUObj.scale;
					_unit = trafficSUObj.unit;
					break;
				case 'flow': //流量
					var trafficSUObj = _getTrafficScaleAndUnitByMaxValue(maxValue,'b');
					_scale = trafficSUObj.scale;
					_unit = trafficSUObj.unit;
					break;
				case 'percent': //百分率
					_scale = 1;
					_unit = '%';
					break;
				case 'number': //数量
					var numberUSObj = _getNumberScaleAndUnitByMaxValue(maxValue, currentLang);
					_scale = numberUSObj.scale;
					_unit = numberUSObj.unit;
					break;
				/*case 'capacity': //容量
				_scale = 1024;
				_unit = 'MB';
				break;*/
			}
		
			return {scale:_scale, unit:_unit};
		}
	
		/**
		* 得到流量刻度比例值
		* 此方法value 为字节数
		*/
		var _getTrafficScaleAndUnitByMaxValue = function(maxValue,endUnit) {
			var unitScale = 1000;
			var obj = {scale:1, unit:''};
		
			if ( maxValue <= Math.pow(unitScale, 1) ) {  //1K(Kilobyte 千字节)
				obj.scale = 1;
			} else if ( maxValue <= Math.pow(unitScale, 2) ) { //1M(Megabyte 兆字节)
				obj.scale = Math.pow(unitScale, 1);
				obj.unit = 'K';
			} else if ( maxValue <= Math.pow(unitScale, 3) ) { //1G(Gigabyte 吉字节)
				obj.scale = Math.pow(unitScale, 2);
				obj.unit = 'M';
			} else if ( maxValue <= Math.pow(unitScale, 4) ) { //1T(Trillionbyte 太字节)
				obj.scale = Math.pow(unitScale, 3);
				obj.unit = 'G';
			} else if ( maxValue <= Math.pow(unitScale, 5) ) { //1P(Petabyte 拍字节)
				obj.scale = Math.pow(unitScale, 4);
				obj.unit = 'T';
			} else if ( maxValue <= Math.pow(unitScale, 6) ) { //1E(Exabyte 艾字节)
				obj.scale = Math.pow(unitScale, 5);
				obj.unit = 'P';
			} else if ( maxValue <= Math.pow(unitScale, 7) ) { //1Z(Zettabyte 泽字节)
				obj.scale = Math.pow(unitScale, 6);
				obj.unit = 'E';
			} else if ( maxValue <= Math.pow(unitScale, 8) ) { //1Y(YottaByte 尧字节)
				obj.scale = Math.pow(unitScale, 7);
				obj.unit = 'Z';
			} else if ( maxValue <= Math.pow(unitScale, 9) ) { //1B(Brontobyte)
				obj.scale = Math.pow(unitScale, 8);
				obj.unit = 'Y';
			} else if ( maxValue <= Math.pow(unitScale, 10) )  {  //1N(NonaByte)
				obj.scale = Math.pow(unitScale, 9);
				obj.unit = 'B';
			} else if ( maxValue <= Math.pow(unitScale, 11) ) {  //1D(DoggaByte)
				obj.scale = Math.pow(unitScale, 10);
				obj.unit = 'N';
			}
		
			obj.unit += endUnit;
			return obj;
		}
	
		/**
		* 得到计数单位
		*/
		var _getNumberScaleAndUnitByMaxValue = function(maxValue, currentLang) {
			var obj = {scale:1, unit:''};
		
			if( currentLang == 'en' ) {
				var unitScale_en = 1000;
			
				if ( maxValue <= Math.pow(unitScale_en, 1) ) {  //1K(Kilobyte 千字节)
					obj.scale = 1;
				} else if ( maxValue <= Math.pow(unitScale_en, 2) ) { //1M(Megabyte 兆字节)
					obj.scale = Math.pow(unitScale_en, 1);
					obj.unit = 'K';
				} else if ( maxValue <= Math.pow(unitScale_en, 3) ) { //1G(Gigabyte 吉字节)
					obj.scale = Math.pow(unitScale_en, 2);
					obj.unit = 'M';
				} else if ( maxValue <= Math.pow(unitScale_en, 4) ) { //1T(Trillionbyte 太字节)
					obj.scale = Math.pow(unitScale_en, 3);
					obj.unit = 'G';
				} else if ( maxValue <= Math.pow(unitScale_en, 5) ) { //1P(Petabyte 拍字节)
					obj.scale = Math.pow(unitScale_en, 4);
					obj.unit = 'T';
				} else if ( maxValue <= Math.pow(unitScale_en, 6) ) { //1E(Exabyte 艾字节)
					obj.scale = Math.pow(unitScale_en, 5);
					obj.unit = 'P';
				} else if ( maxValue <= Math.pow(unitScale_en, 7) ) { //1Z(Zettabyte 泽字节)
					obj.scale = Math.pow(unitScale_en, 6);
					obj.unit = 'E';
				} else if ( maxValue <= Math.pow(unitScale_en, 8) ) { //1Y(YottaByte 尧字节)
					obj.scale = Math.pow(unitScale_en, 7);
					obj.unit = 'Z';
				} else if ( maxValue <= Math.pow(unitScale_en, 9) ) { //1B(Brontobyte)
					obj.scale = Math.pow(unitScale_en, 8);
					obj.unit = 'Y';
				} else if ( maxValue <= Math.pow(unitScale_en, 10) )  {  //1N(NonaByte)
					obj.scale = Math.pow(unitScale_en, 9);
					obj.unit = 'B';
				} else if ( maxValue <= Math.pow(unitScale_en, 11) ) {  //1D(DoggaByte)
					obj.scale = Math.pow(unitScale_en, 10);
					obj.unit = 'N';
				}
			} else if( currentLang == 'zh-cn' ) { //得到中文计数单位
		
				var unitScale_cn = 10;
				var obj = {scale:1, unit:''};
			
				if ( maxValue <= Math.pow(unitScale_cn, 4) ) {  //万
					obj.scale = 1;
				} else if ( maxValue <= Math.pow(unitScale_cn, 8) ) { //亿
					obj.scale = Math.pow(unitScale_cn, 4);
					obj.unit = '万';
				} else if ( maxValue <= Math.pow(unitScale_cn, 12) ) { //兆
					obj.scale = Math.pow(unitScale_cn, 8);
					obj.unit = '亿';
				} else if ( maxValue <= Math.pow(unitScale_cn, 16) ) { //京
					obj.scale = Math.pow(unitScale_cn, 12);
					obj.unit = '兆';
				} else if ( maxValue <= Math.pow(unitScale_cn, 20) ) { //垓 (可根据数量再扩展)
					obj.scale = Math.pow(unitScale_cn, 16);
					obj.unit = '京';
				}
			}
		
			return obj;
		}
	
		this.initScale = function(chartData, displayDataType, currentLang) {
			var maxMinObj = _calcMaxMinValue(chartData);
			_minValue = maxMinObj.minValue;
			_maxValue = maxMinObj.maxValue;
		
			_calcScaleAndUnit(displayDataType, _maxValue, currentLang);
		}
	
		this.getMaxValue = function() {
			return _maxValue;
		}
		this.getMinValue = function() {
			return _minValue;
		}
		this.getScale = function() {
			return _scale;
		}
		this.getUnit = function() {
			return _unit;
		}
	};

	/**
	 * 参数映射
	 */
	HChartUtil.ParamsMapping = {
		periodMapp : function(period) {
			var pv = 1;
			switch( period ) {
				case "current":
					pv = 0;
					break;
				case "hour":
					pv = 1;
					break;
				case "day":
					pv = 2;
					break;
				case "month":
					pv = 3;
					break;
			}
			return pv;
		},
		dataTypeMapp : function(dataType) {
			var dtv = 2;
			switch( dataType ) {
				case "upStream":
					dtv = 0;
					break;
				case "bwStream":
					dtv = 1;
					break;
				case "stream":
					dtv = 2;
					break;
				case "session":
					dtv = 3;
					break;
			}
			return dtv;
		}
	};

})();
